// 780419
// 783169

public class P70 {
	public static void main(String[] args) {
		float min = Integer.MAX_VALUE;
		int minN = 0;
		
		for (int i = 2; i < 10000000; i++) {
			
			if (i % 2 == 0 || i % 3 == 0) {
				continue;
			}
			
			long phi = Util.phi(i);
			
			String phiStr = String.valueOf(phi);
			String oldPhiStr = phiStr;
			String iStr = String.valueOf(i);
						
			if (phiStr.length() == iStr.length()) {
				for (int j = 0; j < iStr.length(); j++) {
					String c = iStr.substring(j, j + 1);
					if (!phiStr.contains(c)) {
						break;
					}
					oldPhiStr = oldPhiStr.replaceFirst(c, "");
				}
				
				if (oldPhiStr.length() == 0) {
					//System.out.println(i + "," + phiStr);
					float ratio = (float) i / (float) phi;
					if (min > ratio) {
						System.out.println(i + "," + phiStr + "," + ratio);	
						min = ratio;
						minN = i;
					}
				}
			}
		}
		
		System.out.println(minN);
	}
}
